package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.PageResult;
import com.heima.mapper.NodeMapper;
import com.heima.mapper.RegionMapper;
import com.heima.pojo.Node;
import com.heima.pojo.Partner;
import com.heima.pojo.Region;
import com.heima.pojo.Vm;
import com.heima.pojo.dto.RegionDTO;
import com.heima.pojo.dto.RegionPageQueryDTO;
import com.heima.pojo.vo.PartnerVo;
import com.heima.pojo.vo.RegionVo;
import com.heima.service.RegionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private NodeMapper nodeMapper;
    @Override
    @Transactional
    public PageResult pageQuery(RegionPageQueryDTO dto) {
        //分页参数
        Page<Region> page = Page.of(dto.getPageIndex(), dto.getPageSize());
        //准备查询条件
        LambdaQueryWrapper<Region> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(dto.getName()!=null ,Region::getName, dto.getName())
                .orderByDesc(Region::getId);

        //执行分页查询
        Page<Region> regionPage = regionMapper.selectPage(page, wrapper);
        List<Region> regionList = regionPage.getRecords();
        List<Long> ids = regionList.stream().map(Region::getId).toList();
        List<RegionVo> regionVos = new ArrayList<>();
        for (Region region : regionList) {
            LambdaQueryWrapper<Node> nodeWrapper = new LambdaQueryWrapper<>();
            nodeWrapper.eq(Node::getRegionId,region.getId());
            RegionVo regionVo = new RegionVo();
            BeanUtils.copyProperties(region,regionVo);
            regionVo.setName(region.getName());
            regionVo.setNodeCount(Integer.parseInt(nodeMapper.selectCount(nodeWrapper).toString()));
            regionVo.setNodeList(nodeMapper.selectBatchIds(ids));
            regionVos.add(regionVo);
        }

        PageResult pageResult = new PageResult(dto.getPageIndex(),dto.getPageSize(),regionPage.getTotal(),regionVos);

        return pageResult;
    }

    @Override
    public void insert(RegionDTO regionDTO) {
        Region region = new Region();
        BeanUtils.copyProperties(regionDTO,region);
        region.setName(regionDTO.getRegionName());
        regionMapper.insert(region);
    }

    /**
     * 修改区域
     * @param regionId
     * @param regionDTO
     */
    @Override
    public void update(Long regionId, RegionDTO regionDTO) {
        Region region = new Region();
        region.setId(regionId);
        region.setName(regionDTO.getRegionName());
        region.setRemark(regionDTO.getRemark());
        regionMapper.updateById(region);
    }

    /**
     * 修改区域
     * @param regionId
     */
    @Transactional
    @Override
    public void delete(Long regionId) {
        regionMapper.deleteById(regionId);
        LambdaQueryWrapper<Node> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(regionId!=null ,Node::getRegionId, regionId);
        List<Node> nodes = nodeMapper.selectList(wrapper);
        List<Long> list = nodes.stream().map(Node::getId).toList();
        if (!CollectionUtils.isEmpty(list)){
           nodeMapper.deleteBatchIds(list);
        }

    }


}
